package com.diankangwo.distribution;

import com.diankangwo.dao.*;
import com.diankangwo.domain.Distribution;
import com.diankangwo.domain.Order;
import com.diankangwo.domain.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 分销
 * Created by wuye on 2017/10/16.
 */
@Service
public class DistributionUtil {

    @Autowired
    private OrderDAO orderDAO;

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private PortSettingDAO portSettingDAO;

    @Autowired
    private PortDAO portDAO;

    @Autowired
    private DistributionDAO distributionDAO;

    /**
     * 分销结算（一级分销）
     */
    public boolean distribution(Order order) {
        ReentrantLock lock = new ReentrantLock();
        try {
            lock.lock();    //加锁
            System.out.println("-------------- fenxiaoSTART-----------------");
            //判断是否有父级ID，获取分销金额
            UserInfo user = userDAO.getUserByUid(order.getUid());//购物用户
            UserInfo userParent = userDAO.find(user.getParentId());//端口分享用户
            if (userParent == null) {
                return false;
            }
            float price = 0;//第一级端口用户抽成
            float price2 = 0;//第二级端口用户抽成

            //执行一级分销
            if (userParent.isPort()) {
                System.out.println("-------------- 用户是端口用户 userParent == true -----------------");
                //统计结算比例
                price = ((float) (order.getTotalFee() / portSettingDAO.findAll().get(0).getCommission()) / 100);
                //用户佣金余额
                userParent.setCommissionbalance(userParent.getCommissionbalance() + price);
                userDAO.saveOrUpdate(userParent);
                //持久化佣金明细
                Distribution distribution = new Distribution();
                distribution.setCreatetime(new Date());
                distribution.setMoney(price);
                distribution.setPrizeuserid(userParent.getId());
                distribution.setPrizeusername(userParent.getName());
                distribution.setType(0);
                distribution.setTriggeruserid(user.getId());
                distributionDAO.insert(distribution);
            }
            //执行二级分销
            if (userParent.getParentId() > 0) {
                System.out.println("-------------- portList.size > 0  -----------------");
                // UserInfo userParent2 = userDAO.find(portList.getResultData().get(0).getParentId());
                UserInfo userParent2 = userDAO.find(userParent.getParentId());
                if (userParent2.isPort()) {
//                    if (userParent2.getId() != userParent2.getParentId()) {
                        System.out.println("-------------- userParent2 == true  -----------------");
                        price = ((float) (order.getTotalFee() / portSettingDAO.findAll().get(0).getCommission()) / 100);
                        price2 = (price / portSettingDAO.findAll().get(0).getCommission()) / 100;
                        userParent2.setCommissionbalance(userParent2.getCommissionbalance() + price2);
                        userDAO.saveOrUpdate(userParent2);
                        //持久化佣金明细
                        Distribution distribution2 = new Distribution();
                        distribution2.setCreatetime(new Date());
                        distribution2.setMoney(price2);
                        distribution2.setPrizeuserid(userParent2.getId());
                        distribution2.setPrizeusername(userParent2.getName());
                        distribution2.setType(0);
                        distribution2.setTriggeruserid(userParent.getId());
                        distributionDAO.insert(distribution2);
//                    }
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            lock.unlock();  //释放锁资源
        }
    }
}
